package org.akomantoso.core.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.io.Externalizable;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.util.MainContentElementUtil;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * The complex type maincontent is used by container elements that can contain basically any other Akoma Ntoso
 * structure.
 * 
 * <pre>
 *  <xsd:complexType name="maincontent">
 * 		<xsd:choice minOccurs="1" maxOccurs="unbounded">
 * 			<xsd:element ref="componentRef"/>
 * 			<xsd:group ref="hierElements"/>
 * 			<xsd:group ref="blockElements"/>
 * 			<xsd:group ref="containerElements"/>
 * 		</xsd:choice>
 * 		<xsd:attributeGroup ref="coreopt"/>
 * 	</xsd:complexType>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 * 
 */
public abstract class MainContent extends CoreOptImpl implements Externalizable {

	// Mandatory
	private final List<MainContentElement> _elements = new ArrayList<MainContentElement>();

	public List<MainContentElement> getElements() {
		return _elements;
	}

	public void addElement(MainContentElement element) {
		_elements.add(element);
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	public <T extends Output> void writeExternal(T out) throws IOException {
		super.writeExternal(out);
		for (int i = 0, n = _elements.size(); i < n; i++) {
			_elements.get(i).writeExternal(out);
		}
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	public <T extends Input> void readExternal(T in) throws IOException {
		super.readExternal(in);
		MainContentElementUtil.read(in, _elements);
	}

	protected final void internalAccept(AKVisitor visitor) {
		for (int i = 0, n = _elements.size(); i < n; i++) {
			_elements.get(i).accept(visitor);
		}
	}
	
}